package com.ruoyi.web.controller.system.fuw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.fuw.domain.FuwPerson;
import com.ruoyi.fuw.service.IFuwPersonService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.util.DataUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwService;
import com.ruoyi.fuw.service.IFuwServiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医疗服务Controller
 * 
 * @author ruoyi
 * @date 2021-11-03
 */
@RestController
@RequestMapping("/fuw/service")
public class FuwServiceController extends BaseController
{
    @Autowired
    private IFuwServiceService fuwServiceService;

    @Autowired
    private IFuwPersonService fuwPersonService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询医疗服务列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwService fuwService)
    {
        startPage();
        List<FuwService> list = new ArrayList<>();
        // 判断是否是超级管理员，否则只能看到自己的购物车
        if (!getUsername().equals("admin")) {
            fuwService.setMedicalId(getUsername());
        }
        list = fuwServiceService.selectFuwServiceList(fuwService);
        return getDataTable(list);
    }

    /**
     * 查询患者/用户person信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:list')")
    @GetMapping("/list/person")
    public AjaxResult listPerson()
    {
        // 查询全部患者/用户信息
        List<FuwPerson> personList = fuwPersonService.selectFuwPersonList(null);
        // 查询服务状态为 1：正在工作中的服务信息 并获取对应的患者的信息进行过滤
        FuwService fuwService = new FuwService();
        fuwService.setServiceState("1");
        List<FuwService> serviceList = fuwServiceService.selectFuwServiceList(fuwService);

        // 过滤掉正在服务状态的患者/用户
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for(FuwPerson fuwPerson:personList){
            Boolean server = false;
            for(FuwService fuwServiceNow:serviceList){
                // 判断改患者/用户是否在服务中
                if(fuwPerson.getPersonId().equals(fuwServiceNow.getPersonId())){
                    server = true;
                }
            }
            if(!server){
                Map<String, Object> mapNew = new HashMap<>();
                mapNew.put("value", fuwPerson.getPersonId());
                mapNew.put("label", fuwPerson.getPersonName());
                listMap.add(mapNew);
            }
        }

        return AjaxResult.success(listMap);
    }

    /**
     * 查询医疗工作者medical/sysUser信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:list')")
    @GetMapping("/list/medical")
    public AjaxResult listMedical()
    {
        // 查询全部医疗工作者信息
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(113L);
        sysUser.setStatus("0");
        List<SysUser> userList = userService.selectList(sysUser);

        // 过滤掉正在服务状态的医疗工作者
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
        for(SysUser sysUserNow:userList){
            Map<String, Object> mapNew = new HashMap<>();
            mapNew.put("value", sysUserNow.getUserName());
            mapNew.put("label", sysUserNow.getNickName());
            listMap.add(mapNew);
        }

        return AjaxResult.success(listMap);
    }

    /**
     * 导出医疗服务列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:export')")
    @Log(title = "医疗服务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwService fuwService)
    {
        List<FuwService> list = fuwServiceService.selectFuwServiceList(fuwService);
        ExcelUtil<FuwService> util = new ExcelUtil<FuwService>(FuwService.class);
        return util.exportExcel(list, "医疗服务数据");
    }

    /**
     * 获取医疗服务详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:query')")
    @GetMapping(value = "/{serviceId}")
    public AjaxResult getInfo(@PathVariable("serviceId") String serviceId)
    {
        return AjaxResult.success(fuwServiceService.selectFuwServiceByServiceId(serviceId));
    }

    /**
     * 新增医疗服务
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:add')")
    @Log(title = "医疗服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FuwService fuwService)
    {
        // 自定义格式UI
        String uuidStr = DataUtils.fuwUUID("Ser");
        fuwService.setServiceId(uuidStr);

        return toAjax(fuwServiceService.insertFuwService(fuwService));
    }

    /**
     * 修改医疗服务
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:edit')")
    @Log(title = "医疗服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FuwService fuwService)
    {
        return toAjax(fuwServiceService.updateFuwService(fuwService));
    }

    /**
     * 删除医疗服务
     */
    @PreAuthorize("@ss.hasPermi('fuw:service:remove')")
    @Log(title = "医疗服务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serviceIds}")
    public AjaxResult remove(@PathVariable String[] serviceIds)
    {
        return toAjax(fuwServiceService.deleteFuwServiceByServiceIds(serviceIds));
    }
}
